function Node(val) {
  this.value = val;
  this.left = null;
  this.right = null;
}

let a1 = new Node('a');
let b1 = new Node('b');
let c1 = new Node('c');
let d1 = new Node('d');
let e1 = new Node('e');
let f1 = new Node('f');
let g1 = new Node('g');

a1.left = b1;
a1.right = c1;
b1.left = d1;
b1.right = e1;
c1.left = f1;
c1.right = g1;


let a2 = new Node('a');
let b2 = new Node('b');
let c2 = new Node('c');
let d2 = new Node('d');
let e2 = new Node('e');
let f2 = new Node('f');
let g2 = new Node('h');

a2.left = b2;
a2.right = c2;
b2.left = d2;
b2.right = e2;
c2.left = f2;
c2.right = g2;

function compare(leftNode, righNode) {
  if (leftNode == null || righNode == null) return true;
  if (leftNode == null && righNode != null || leftNode != null && righNode == null || leftNode.value != righNode.value) return false;
  let leftRes = compare(leftNode.left, righNode.left);
  let rightRes = compare(leftNode.right, righNode.right);
  return leftRes && rightRes;
}

console.log(compare(a1, a2));